﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SportStore.Domain.Entities;
using SportStore.WebUI.Controllers;
using SportStore.WebUI.Models;

namespace SportStore.UnitTests
{
    [TestFixture]
    public class ShoppingCart
    {
        [Test]
        public void Cart_Starts_Empty()
        {
            new Cart().Lines.Count.ShouldEqual(0);
        }

        [Test]
        public void Cart_Combines_Lines_With_Same_Product()
        {
            Product p1 = new Product { ProductId = 1 };
            Product p2 = new Product { ProductId = 2 };

            var cart = new Cart();

            cart.AddItem(p1, 1);
            cart.AddItem(p2, 2);
            cart.AddItem(p2, 10);

            cart.Lines.Count.ShouldEqual(2);
            cart.Lines.First(x => x.Product.ProductId == 1).Quantity.ShouldEqual(1);
            cart.Lines.First(x => x.Product.ProductId == 2).Quantity.ShouldEqual(12);
        }

        [Test]
        public void Cart_Can_Be_Clear()
        {
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);
            cart.Clear();
            cart.Lines.Count.ShouldEqual(0);
        }

        [Test]
        public void Cart_TotalValue_Is_Sum_Of_Price_Times_Quantity()
        {
            Cart cart = new Cart();
            cart.AddItem(new Product { ProductId = 1, Price = 5 }, 10);
            cart.AddItem(new Product { ProductId = 2, Price = 2.1M }, 3);
            cart.AddItem(new Product { ProductId = 3, Price = 1000 }, 1);

            cart.ComputeTotalValue().ShouldEqual(1056.3M);
        }

        [Test]
        public void Can_Add_Product_To_Cart()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { ProductId = 1 },
                new Product { ProductId = 2 }
                );

            var cartController = new CartController(mockProductsRepository);
            var cart = new Cart();

            cartController.AddToCart(cart, 1, null);

            cart.Lines.Count.ShouldEqual(1);
            cart.Lines[0].Product.ProductId.ShouldEqual(1);
        }

        public void After_Adding_Product_To_Cart_User_Goes_To_Your_Cart_Screen()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { ProductId = 1 },
                new Product { ProductId = 2 }
                );

            var cartController = new CartController(mockProductsRepository);

            var result = cartController.AddToCart(new Cart(), 1, "someReturnUrl");

            result.ShouldBeRedirectTo(new
            {
                action = "Index",
                returnUrl = "someReturnUrl"
            });
        }

        [Test]
        public void Can_View_Cart_Contents()
        {
            var cart = new Cart();
            var result = new CartController(null).Index(cart, "someReturnUrl");

            var viewModel = (CartIndexViewModel)result.ViewData.Model;
            viewModel.Cart.ShouldEqual(cart);
            viewModel.ReturnUrl.ShouldEqual("someReturnUrl");
        }
    }
}
